package hotel.switchs.oversea.biz.controller;

import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.SwitchConstants;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.vo.meituan.request.room.CompareRoomForFpVO;
import hotel.switchs.oversea.api.VO.request.GetRoomListVo;
import hotel.switchs.oversea.api.VO.request.SetHotelUpVo;
import hotel.switchs.oversea.api.entity.SwitchHotelUpEntity;
import hotel.switchs.oversea.biz.config.service.RedisService;
import hotel.switchs.oversea.biz.mapper.SwitchHotelUpMapper;
import hotel.switchs.oversea.biz.service.SwitchHotelUpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author lmf
 * @Date 2020/6/2 15:04
 * 房价 相关控制器
 **/
@RestController
@Slf4j
@Api(value = "switchHotelUpController", tags = {"房价相关控制器"})
@RequestMapping("/switchHotelUp")
public class SwitchHotelUpController {

    @Autowired
    private SwitchHotelUpService switchHotelUpService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private SwitchHotelUpMapper switchHotelUpMapper;

    @ApiOperation(value = "获取上架酒店和产品信息", notes = "必须经过auth授权")
    @PostMapping("/getUpHotelGoodsData")
    public Result<Object> getUpHotelGoodsData() {
        return switchHotelUpService.getUpHotelGoodsData();
    }

    @ApiOperation(value = "获取上架酒店和产品信息", notes = "必须经过auth授权")
    @PostMapping("/getUpHotelGoodsDataForBasicRoom")
    public Result<Object> getUpHotelGoodsDataForBasicRoom() {
        return switchHotelUpService.getUpHotelGoodsDataForBasicRoom();
    }

    @ApiOperation(value = "获取上架酒店和产品信息指定酒店", notes = "必须经过auth授权")
    @PostMapping("/getUpHotelGoodsDataByHotelId")
    public Result<Object> getUpHotelGoodsDataByHotelId(@RequestBody Map map) {
        Integer mtHotelId = (Integer) map.get("mtHotelId");
        return switchHotelUpService.getUpHotelGoodsDataByHotelId(mtHotelId);
    }

    @ApiOperation(value = "获取商家酒店信息升序返回", notes = "必须经过auth授权")
    @PostMapping("/getUpHotelAsc")
    public Result<Object> getUpHotelAsc() {
        return switchHotelUpService.getUpHotelAsc();
    }

    /***
    * @Author: lmf
    * @Date: 2020/12/23
     * 返回酒店列表
    */
    @ApiOperation(value = "获取售卖酒店信息供美团接口升序返回", notes = "必须经过auth授权")
    @PostMapping("/getUpHotelRpOrFtAsc")
    public Result<Object> getUpHotelRpOrFtAsc(@RequestBody Map map) {
        Integer genre = (Integer) map.get("genre");
        return switchHotelUpService.getUpHotelRpOrFtAsc(genre);
    }

    @ApiOperation(value = "获取商家酒店信息降序返回", notes = "必须经过auth授权")
    @PostMapping("/getUpHotelDesc")
    public Result<Object> getUpHotelDesc() {
        return switchHotelUpService.getUpHotelDesc();
    }


    @PostMapping("/setRedisByUpHotelGoodsId")
    @ApiOperation(value = "设置携程-美团酒店商品对应缓存", notes = "必须经过auth授权")
    public Result<Boolean> setRedisByUpHotelGoodsId() {
        return switchHotelUpService.setRedisByUpHotelGoodsId();
    }

    @PostMapping("/delRedisByUpHotelGoodsId")
    @ApiOperation(value = "清除携程-美团酒店商品对应缓存", notes = "必须经过auth授权")
    public Result<Boolean> delRedisByUpHotelGoodsId() {
        return switchHotelUpService.delRedisByUpHotelGoodsId();
    }

    /**
     * 清除缓存和下线售卖房型
     */
    @PostMapping("/delErrorRomId")
    @ApiOperation(value = "清除缓存和下线售卖房型", notes = "必须经过auth授权")
    public Result<Object> delErrorRomId() {
        return switchHotelUpService.delErrorRomId();
    }


    /**
     *
     * @author: lmf
     * @time: 2020/10/27 16:36
     * 导入酒店数据,开启酒店售卖
     */
    @PostMapping("/submitUploadSale")
    @ApiOperation(value = "导入酒店数据,开启酒店售卖", notes = "必须经过auth授权")
    public Result<Object> submitUploadSale(@RequestParam("excelFile") MultipartFile excelFile) {
        if(excelFile.isEmpty()){
            return new Result<>("上传失败");
        }
        return  switchHotelUpService.submitUploadSale(excelFile,1);
    }

    /***
     * @Author: lmf
     * @Date: 2020/9/21
     * 导入在售的酒店数据
     */
    @PostMapping("/upExecel")
    @ApiOperation(value = "导入在售的酒店数据",notes = "必须经过oauth授权")
    public Result<Object> upExecel(@RequestParam("excelFile") MultipartFile excelFile) throws IOException {
        if(excelFile.isEmpty()){
            return new Result<>("上传失败");
        }
        return switchHotelUpService.upExecel(excelFile);
    }
    /***
     * @Author: lmf
     * @Date: 2020/9/23
     * 开启全量售卖
     */
    @PostMapping("/upHotelAll")
    @ApiOperation(value = "控制批量上架和批量下架功能",notes = "必须经过oauth授权")
    public Result<Object> aspupHotelAll(@RequestBody SetHotelUpVo setHotelUpVo){
        return switchHotelUpService.upHotelAllNew(setHotelUpVo);
    }
    /**
     * 批量下架功能
     */
    @PostMapping("/downHotelAll")
    @ApiOperation(value = "控制批量下架功能",notes = "必须经过oauth授权")
    public Result<Object> aspdownHotelAll(@RequestBody SetHotelUpVo setHotelUpVo){
        return switchHotelUpService.downHotelAll(setHotelUpVo);
    }
    /**
     * @Author: lmf
     * @Date: 2020/9/21
     * 获取已经上架的酒店列表
     */
    @PostMapping("/getHotelList")
    @ApiOperation(value = "获取已经上架的酒店列表",notes = "必须经过oauth授权")
    public Result<Object> getHotelList(@RequestBody GetRoomListVo getRoomListVo){
        return  switchHotelUpService.getHotelList(getRoomListVo);
    }


    /**
     * @author: lmf
     * @date: 2020/8/14 13:40
     * 后台控制酒店售卖状态
     */
    @PostMapping("/modifyHotelStatus")
    @ApiOperation(value = "后台控制酒店售卖状态",notes = "必须经过oauth授权")
    public Result<Object> aspmodifyHotelStatus(@RequestBody SetHotelUpVo setHotelUpVo){
        return switchHotelUpService.modifyHotelStatus(setHotelUpVo);
    }
    /**
     *
     * @author: lmf
     * @time: 2020/10/15 16:00
     * 上架的酒店
     */
    @GetMapping("/runHotel")
    @ApiOperation(value="上架的酒店循环跑",notes = "必须经过oauth授权")
    public Result<Object> runHotel(){
        return switchHotelUpService.runHotel();
    }


    /***
     * @Author: lmf
     * @Date: 2020/9/30
     * 上架缓存清空
     */
    @PostMapping("/cacheOperator")
    @ApiOperation(value = "控制批量上架和批量下架功能",notes = "必须经过oauth授权")
    public Result<Object> cacheOperator(@RequestBody SetHotelUpVo setHotelUpVo){
        return switchHotelUpService.cacheOperator(setHotelUpVo);
    }

    /**
     *
     * @author: lmf
     * @time: 2020/10/16 17:14
     * 清除所有缓存
     */
    @GetMapping("/removeALl")
    @ApiOperation(value="removeALl",notes = "必须经过oauth授权")
    public Result<Object> removeALl(){
        return switchHotelUpService.removeALl();
    }


    @ApiOperation(value = "获取上架酒店和产品信息", notes = "必须经过auth授权")
    @PostMapping("/getUpHotelGoodsDataFp")
    public Result<List<CompareRoomForFpVO>> getUpHotelGoodsDataFp() {
        return switchHotelUpService.getUpHotelGoodsDataFp();
    }
    /**
     * 添加酒店到缓存中
     */
    @ApiOperation(value = "添加酒店到缓存中", notes = "必须经过auth授权")
    @GetMapping("/addHotel")
    public Result<Object> addHotel() {
        return switchHotelUpService.addHotel();
    }

    /**
     * 添加美团的房型到缓存中key:mt_hotel_id+mt_room_id 作为效验房型是否失效的凭证
     */
    @ApiOperation(value = "添加美团的房型到缓存中key:mt_hotel_id+mt_room_id 作为效验房型是否失效的凭证", notes = "必须经过auth授权")
    @GetMapping("/addHotelRoomCache")
    public Result<Object> addHotelRoomCache() {
        return switchHotelUpService.addHotelRoomCache();
    }

    /**
     * 添加美团的房型到缓存中key:mt_hotel_id+mt_goods_id+餐食 作为效验餐食是否变更的依据
     */
    @ApiOperation(value = "添加美团的房型到缓存中key:mt_hotel_id+mt_goods_id+餐食 作为效验餐食是否变更的依据", notes = "必须经过auth授权")
    @GetMapping("/addHotelGoodBreakfastCache")
    public Result<Object> addHotelGoodBreakfastCache() {
        return switchHotelUpService.addHotelGoodBreakfastCache();
    }

    /**
     * 添加相关操作的缓存
     */
    @ApiOperation(value = "添加相关操作的缓存", notes = "必须经过auth授权")
    @GetMapping("/addAllCache")
    public Result<Object> addAllCache(@RequestParam(value = "genre",defaultValue = "0")Integer genre) {
        return switchHotelUpService.addAllCache(genre);
    }

    /***
    * @Author: lmf
    * @Date: 2020/12/2
    */
    @ApiOperation(value = "添加相关操作的缓存", notes = "必须经过auth授权")
    @GetMapping("/getCacheValue")
    public Result<Object> getCacheValue(@RequestParam(value = "mtHotelId",defaultValue = "")String mtHotelId, @RequestParam(value = "mtRoomId",defaultValue = "")String mtRoomId) {

        String key = CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST + mtHotelId + "_" + mtRoomId+"_1";
        if(redisService.exists(key)){
            return new Result<>(redisService.get(key));
        }
        String key2 = CommonConstants.SWITCH_CREATE_ROOM_BREAKFAST + mtHotelId + "_" + mtRoomId+"_0";
        if(redisService.exists(key2)){
            return new Result<>(redisService.get(key2));
        }
        return new Result<>();
    }

    /***
    * @Author: lmf
    * @Date: 2020/12/3
    */
    @ApiOperation(value = "把酒店加入緩存進行推送", notes = "必须经过auth授权")
    @GetMapping("/setHotelId")
    public Result<Object> setHotelId() {
        List<SwitchHotelUpEntity> switchHotelUpEntities=switchHotelUpMapper.selectAll();
        for(SwitchHotelUpEntity switchHotelUpEntity:switchHotelUpEntities){
            redisService.lPush(SwitchConstants.CREATE_SUB_FOR_ROOM_MASTER_JOB,String.valueOf(switchHotelUpEntity.getHotelId()));
            log.info("创建子酒店 SwitchConstants.CREATE_SUB_FOR_ROOM_MASTER_JOB.size={},{}",redisService.listSize(SwitchConstants.CREATE_SUB_FOR_ROOM_MASTER_JOB));
            redisService.lPush(SwitchConstants.CREATE_SUB_FOR_ROOM_STATIC_JOB,switchHotelUpEntity.getMasterHotelId()+"_"+switchHotelUpEntity.getHotelId());
            log.info("创建子酒店 SwitchConstants.CREATE_SUB_FOR_ROOM_STATIC_JOB.size={},{}",redisService.listSize(SwitchConstants.CREATE_SUB_FOR_ROOM_STATIC_JOB),switchHotelUpEntity.getMasterHotelId()+"_"+switchHotelUpEntity.getHotelId());
        }
        return new Result<>();
    }

    /***
    * @Author: lmf
    * @Date: 2020/12/8
     * 全量更新出售中的酒店
    */
    @ApiOperation(value = "全量更新出售中的酒店", notes = "必须经过auth授权")
    @GetMapping("/batchUpdateRoomPriceAndStatus")
    public Result<Object> batchUpdateRoomPriceAndStatus(){
        switchHotelUpService.batchUpdateRoomPriceAndStatus();
        return new Result<>();
    }
    /***
    * @Author: lmf
    * @Date: 2020/12/15
     * 定点售卖房型
    */
    @ApiOperation(value = "定点售卖房型",notes = "必须经过auth授权")
    @GetMapping("/bookTime")
    public Result<Object> bookTime(){
        switchHotelUpService.batchUpdateRoomPriceAndStatus();
        return new Result<>();
    }

    /***
    * @Author: lmf
    * @Date: 2020/12/31
     * 0-5点暂时关闭没有订单的酒店
     * 针对夜间只售卖有订单的酒店
    */
    @ApiOperation(value = "针对夜间只售卖有订单的酒店",notes = "必须经过auth授权")
    @GetMapping("/bookTimeCloseNoOrderHotelList")
    public Result<Object> bookTimeCloseNoOrderHotelList(){
        return switchHotelUpService.bookTimeCloseNoOrderHotelList();
    }

    /***
    * @Author: lmf
    * @Date: 2020/12/31
     * 5点-6点开启停售的酒店恢复上线
    */
    @ApiOperation(value = "针对夜间停售酒店重新上架",notes = "必须经过auth授权")
    @GetMapping("/bookTimeStartNoOrderHotelList")
    public Result<Object> bookTimeStartNoOrderHotelList(){
        return switchHotelUpService.bookTimeStartNoOrderHotelList();
    }



}
